<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 被围绕的区域 -->
    <script>
      var solve = function (board) {
        let m = board.length,
          n = board[0].length
        const dfs = (r, c) => {
          if (r < 0 || r >= m || c < 0 || c >= n || board[r][c] !== 'O') return
          board[r][c] = 'A'
          dfs(r, c + 1)
          dfs(r, c - 1)
          dfs(r + 1, c)
          dfs(r - 1, c)
        }

        //取第一列和最后一列的所有O
        for (let i = 0; i < n; i++) {
          dfs(0, i)
          dfs(m - 1, i)
        }
        //取第一行和最后一行的所有O
        for (let i = 1; i < m - 1; i++) {
          dfs(i, 0)
          dfs(i, n - 1)
        }
        for (let i = 0; i < m; i++) {
          for (let j = 0; j < n; j++) {
            if (board[i][j] === 'A') {
              board[i][j] = 'O'
            } else {
              board[i][j] = 'X'
            }
          }
        }
      }
      console.log(solve([['X']]))
    </script>
  </body>
</html>
